const jwt = require('jsonwebtoken');

const secretKey = 'a';
const secretKeyx = 'b';

const createAccessToken = (user) => {
  return jwt.sign(
    { username: user.username, uid: user.uid, nickname: user.nickname },
    secretKey,
    { expiresIn: 60 * 30 },
  );
};

const createRefreshToken = (user) => {
  return jwt.sign({ username: user.username }, secretKeyx, { expiresIn: '7d' });
};
//token校验
const verifyToken = async function (req, res, next) {
    const token = req.headers.authorization.split(' ')[1]
    jwt.verify(token, secretKey, function (err, decoded) {
        if (err) {
            res.status(401)
            return res.send({ code: 4004, msg: 'token无效' })
        }
        req.data = decoded
        next()
    })
}
module.exports = { secretKey, secretKeyx, verifyToken, createAccessToken, createRefreshToken }